package com.vital.mshopping;

import com.vital.dao.Bookmark;
import com.vital.dao.History;
import com.vital.dao.Location;
import com.vital.service.DatabaseService;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class DetailLocationActivity extends Activity {
	Location location;
	TextView tvTitle;
	TextView tvAddress;
	TextView tvAccuracy;
	TextView tvPhone;
	Button btnShowMap;
	Button btnAddBookmark;
	Context mContext;
	boolean isAdded = true;
	boolean isOnCreate = true;
	boolean isBackView = false;
	long bookmarkId;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_location);
		isOnCreate = true;
		
		mContext = this.getApplicationContext();
		isAdded = true;
		
		//---Get widget---
		tvTitle = (TextView)findViewById(R.id.tvTitle);
		tvAddress = (TextView)findViewById(R.id.tvAddress);
		tvAccuracy = (TextView)findViewById(R.id.tvAccuracy);
		tvPhone = (TextView)findViewById(R.id.tvPhone);
		btnShowMap = (Button)findViewById(R.id.btnShowMap);
		btnAddBookmark = (Button)findViewById(R.id.btnAddBookmark);
		
		btnShowMap.setOnClickListener(btnShowMap_click);
		btnAddBookmark.setOnClickListener(btnAddBookmark_click);
		
		//---Retrieve Location from VenueActivity---
		location = new Location();
		Intent intent = this.getIntent();
		Bundle b = intent.getExtras();
		
		//---Get Location from LocationActivity---
		if (b.getSerializable("location_detail") != null) {
			location = (Location)b.getSerializable("location_detail");
			insertHistory(location);
		}
		
		//---Get Location from BookmarkActivity---
		if (b.getSerializable("ObjBookmark") != null) {
			Bookmark bookmark = (Bookmark)b.getSerializable("ObjBookmark");
			location.setId(bookmark.getLocationId());
			location.setAddress(bookmark.getAddress());
			location.setAccuracy(bookmark.getAccuracy());
			location.setCategory(bookmark.getCategory());
			location.setLat(bookmark.getLat());
			location.setLng(bookmark.getLng());
			location.setPhone(bookmark.getPhone());
			location.setName(bookmark.getName());
			bookmarkId = bookmark.getId();
			//---Set remove bookmark---
			btnAddBookmark.setText("Remove bookmark".toString());
			isAdded = false;
			isBackView = true;
		}
		
		//---Get Location from BookmarkActivity---
		if (b.getSerializable("ObjHistory") != null) {
			History history = (History)b.getSerializable("ObjHistory");
			location.setId(history.getLocationId());
			location.setAddress(history.getAddress());
			location.setAccuracy(history.getAccuracy());
			location.setCategory(history.getCategory());
			location.setLat(history.getLat());
			location.setLng(history.getLng());
			location.setPhone(history.getPhone());
			location.setName(history.getName());
			// bookmarkId = history.getId();
			//---Set remove bookmark---
			//btnAddBookmark.setText("Remove bookmark".toString());
			//isAdded = false;
			//isBackView = true;
		}
		
		
		//---Get Location from MapViewActivity---
		if (b.getSerializable("mapObjLocation") != null) {
			location = (Location)b.getSerializable("mapObjLocation");
			//---insert this to history---
			insertHistory(location);
		}
		
		//---Set value for widget---
		fillData(location);
	}

	private void fillData(Location location) {
		tvTitle.setText(location.getName().toString());
		tvAddress.setText(location.getAddress().toString());
		tvAccuracy.setText(location.getAccuracy().toString());
		tvPhone.setText(location.getPhone().toString());
	}
	
	/**
	 * insert History
	 */
	private void insertHistory(Location location) {
		DatabaseService db = new DatabaseService(this);
		db.open();
		db.insertHistory(location.getId());
		db.close();
	}
	
	/**
	 * btnShowMap clicked
	 */
	private OnClickListener btnShowMap_click = new OnClickListener() {

		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			Bundle b = new Bundle();
			Intent intent = new Intent(getApplicationContext(), MapViewActivity.class);
			b.putSerializable("map_location", location);
			intent.putExtras(b);
			startActivity(intent);
		}
	};
	
	/**
	 * btnAddBookmark clicked
	 */
	private OnClickListener btnAddBookmark_click = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			DatabaseService db = new DatabaseService(mContext);
			db.open();
			if (isAdded) {
				bookmarkId = db.insertBookMark(location.getId());
				if (bookmarkId > -1) {
					showMessage("Successful", "Location has been added to Bookmark.");
					btnAddBookmark.setText("Remove bookmark");
					isAdded = false;
				}
			} else {
				if (db.removeBookmark((int)bookmarkId)) {
					if (isBackView) {
						Bundle b = new Bundle();
						Intent intent = new Intent(getApplicationContext(), BookmarkActivity.class);
						b.putInt("removed_bookmarkId", (int)bookmarkId);
						intent.putExtras(b);
						startActivity(intent);
						
						//finish();
					} else {
						showMessage("Successful", "Location has been removed from Bookmark.");
						isAdded = true;
						btnAddBookmark.setText("Add bookmark");
					}
				}
			}
			db.close();
		}
	};
	
	/**
	 * Dialog message
	 */
	private void showMessage(String title, String msg) {
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setTitle(title);
		dialog.setMessage(msg);
		dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				//---Hide dialog---
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_detail_location, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
			case R.id.location_menu_bookmark:
				Intent book_intent = new Intent(getApplicationContext(), BookmarkActivity.class);
				startActivity(book_intent);
				break;
	
			case R.id.location_menu_history:
				Intent his_intent = new Intent(getApplicationContext(), HistoryActivity.class);
				startActivity(his_intent);
				break;
		}
		return super.onOptionsItemSelected(item);
		
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (isOnCreate) {
			isOnCreate = false;
		} else {
			isAdded = true;
			btnAddBookmark.setText("Add bookmark");
		}
	}
	
	

}
